import { MultiServerMCPClient } from '@langchain/mcp-adapters';
// import { ChatOpenAI } from '@langchain/openai';
// import { createReactAgent } from '@langchain/langgraph/prebuilt';

export class UseMcpServer {
  private constructor() {}

  //https://www.npmjs.com/package/@langchain/mcp-adapters
  static getClient() {
    // Create client and connect to server
    const client = new MultiServerMCPClient({
      // Global tool configuration options
      // Whether to throw on errors if a tool fails to load (optional, default: true)
      throwOnLoadError: true,
      // Whether to prefix tool names with the server name (optional, default: false)
      prefixToolNameWithServerName: false,
      // Optional additional prefix for tool names (optional, default: "")
      additionalToolNamePrefix: '',

      // Use standardized content block format in tool outputs
      useStandardContentBlocks: true,

      // Server configuration
      mcpServers: {
        weather: {
          // name: 'weather',
          type: 'stdio',
          // isActive: true,
          // longRunning: true,
          // tags: [],
          stderr: 'inherit',
          command: 'node',
          args: ['/Users/wangkang/Desktop/study/mcp/project-01/dist/index.js'],
          restart: {
            enabled: true,
            maxAttempts: 5,
            delayMs: 2000,
          },
          // outputHandling: {
          //   text: 'artifact',
          // },
        },
        // adds a STDIO connection to a server named "math"
        // math: {
        //   transport: 'stdio',
        //   command: 'npx',
        //   args: ['-y', '@modelcontextprotocol/server-math'],
        //   // Restart configuration for stdio transport
        //   restart: {
        //     enabled: true,
        //     maxAttempts: 3,
        //     delayMs: 1000,
        //   },
        // },
        // here's a filesystem server
        // filesystem: {
        //   transport: 'stdio',
        //   command: 'npx',
        //   args: ['-y', '@modelcontextprotocol/server-filesystem'],
        // },
        // Sreamable HTTP transport example, with auth headers and automatic SSE fallback disabled (defaults to enabled)
        // weather: {
        //   url: 'https://example.com/weather/mcp',
        //   headers: {
        //     Authorization: 'Bearer token123',
        //   },
        //   automaticSSEFallback: false,
        // },
        // OAuth 2.0 authentication (recommended for secure servers)
        // 'oauth-protected-server': {
        //   url: 'https://protected.example.com/mcp',
        //   authProvider: new MyOAuthProvider({
        //     // Your OAuth provider implementation
        //     redirectUrl: 'https://myapp.com/oauth/callback',
        //     clientMetadata: {
        //       redirect_uris: ['https://myapp.com/oauth/callback'],
        //       client_name: 'My MCP Client',
        //       scope: 'mcp:read mcp:write',
        //     },
        //   }),
        //   // Can still include custom headers for non-auth purposes
        //   headers: {
        //     'User-Agent': 'My-MCP-Client/1.0',
        //   },
        // },
        // how to force SSE, for old servers that are known to only support SSE (streamable HTTP falls back automatically if unsure)
        // github: {
        //   transport: 'sse', // also works with "type" field instead of "transport"
        //   url: 'https://example.com/mcp',
        //   reconnect: {
        //     enabled: true,
        //     maxAttempts: 5,
        //     delayMs: 2000,
        //   },
        // },
      },
    });
    return client;
  }

  static async getTools() {
    const tools = await this.getClient().getTools();
    return tools;
  }
}
